home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 6_3.lha / 6_3 / 6_3a.c next >
C/C++ Source or Header  |  1993-08-08  |  694b  |  33 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / substring operator, second version
  6. / exercise 6.3
  7. ubstring string::operator()(int index, int count)
  8.  
  9.    char *s1 = p->s;
  10.    int s1len = strlen(s1);
  11.  
  12.    // convert left index, if necessary
  13.    if (index < 0)
  14. index += s1len;
  15.  
  16.    // left index past end of string
  17.    else if (index >= s1len)
  18.        {
  19. substring sst(s1+s1len, 0, this);
  20. return sst;
  21. }
  22.  
  23.    // convert count, if necessary
  24.    int numleft = s1len - index;
  25.    if (count > numleft || count < 0)
  26. count = numleft;
  27.  
  28.    // copy the substring
  29.    substring sst(s1+index, count, this);
  30.    return sst;
  31.  
  32.  
  33.